package com.generator.marketManage.orderVerify.controller;

import com.generator.aboutUser.user.pojo.Users;
import com.generator.marketManage.orderVerify.pojo.Order;
import com.generator.marketManage.orderVerify.pojo.OrderCondition;
import com.generator.marketManage.orderVerify.pojo.OrderDetail;
import com.generator.marketManage.orderVerify.service.impl.OrderVerifyService;
import com.generator.util.DateFormatUtil;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/orderVerify")
public class OrderVerifyController {

    @Autowired
    private OrderVerifyService orderVerifyService;

    public OrderVerifyService getOrderVerifyService() {
        return orderVerifyService;
    }

    public void setOrderVerifyService(OrderVerifyService orderVerifyService) {
        this.orderVerifyService = orderVerifyService;
    }

    @RequestMapping("/queryOrder")
    @ResponseBody
    //条件查询处于审核中的订单
    public PageModel<Order> queryOrder(HttpServletRequest request, OrderCondition condition,PageModel<Order> pageModel){
        String orderid = request.getParameter("orderid");
        String customname = request.getParameter("customname");
        String stime = request.getParameter("startTime");
        String etime = request.getParameter("endTime");
        String minprice = request.getParameter("minprice");
        String maxprice = request.getParameter("maxprice");
        String pageNum = request.getParameter("pageNum");
        if (pageNum!=null){
            int i = Integer.parseInt(pageNum);
            pageModel.setPageNum(i);
        }

//        Users user = (Users) request.getSession().getAttribute("user");
//        if (user!=null){
//            Integer uId = user.getUId();
//            condition.setLoaduserid(uId);
//        }

        Date startTime = null;
        if (stime!=null&&!stime.equals("")) {
            startTime = DateFormatUtil.stringParseToDate(stime);
        }
        Date endTime = null;
        if (etime!=null&&!etime.equals("")){
            endTime = DateFormatUtil.stringParseToDate(etime);
        }

        condition.setCustomname(customname);
        condition.setOrderid(orderid);
        condition.setMinprice(minprice);
        condition.setMaxprice(maxprice);
        condition.setStartTime(startTime);
        condition.setEndTime(endTime);

        PageModel<Order> m = orderVerifyService.queryOrder(condition, pageModel);
        if (m!=null&&!m.getData().isEmpty()){
            return m;
        }
        return null;
    }


    @RequestMapping("/putOrderId")
    @ResponseBody
    //存储orderid到session
    public void putOrderId(HttpServletRequest request){
        String orderid = request.getParameter("orderid");
        request.getSession().setAttribute("orderid",orderid);
    }

    //跳转审核页面获取订单信息
    @RequestMapping("/queryOrderById")
    @ResponseBody
    public List<Order> queryOrderById(HttpServletRequest request, OrderCondition condition){
        String orderid = (String) request.getSession().getAttribute("orderid");
        if (orderid!=""&&orderid!=null){
            condition.setOrderid(orderid);
            return orderVerifyService.queryOrderById(condition);
        }
        return null;
    }

    //通过订单号查询订单商品详情
    @RequestMapping("/queryOrderDetailByOrderID")
    @ResponseBody
    public List<OrderDetail> queryOrderDetailById(OrderDetail orderDetail,HttpServletRequest request){
        String orderid = (String) request.getSession().getAttribute("orderid");
        if (orderid!=""&&orderid!=null){
            orderDetail.setOrderid(orderid);
            return orderVerifyService.queryOrderDetailById(orderDetail);
        }
        return null;
    }

//    修改订购单审核意见及状态
    @RequestMapping("/updateOpinion")
    @ResponseBody
    public Integer updateOpinion(Order order,HttpServletRequest request){
        String opinion = request.getParameter("opinion");
        String status = request.getParameter("status");
        String orderid = (String) request.getSession().getAttribute("orderid");
            order.setOrderid(orderid);
            order.setOpinion(opinion);
            order.setStatus(status);
            return orderVerifyService.updateOpinion(order);
    }
}
